#include "GamePlayScene.h"
#include "Utils.h"

USING_NS_CC;

Scene* GamePlayScene::createScene()
{
    auto scene = Scene::create();   
    auto layer = GamePlayScene::create();
	scene->addChild(layer);
    return scene;
}

bool GamePlayScene::init()
{
    if ( !Layer::init() )  return false;      
	
	Layer *uiLayer = Layer::create();
	Layout* uiFromJson = dynamic_cast<Layout*>(cocostudio::GUIReader::getInstance()->widgetFromJsonFile("Editor/UI/action_phase.json"));
	uiLayer->addChild(uiFromJson);
	this->addChild(uiLayer);

	currentQuestionLeft = currentQuestionRight = 1;
	leftScore = rightScore = totalScore = 0;
	numLife = MAX_LIFE;

	//Timer
	ArmatureDataManager::getInstance()->addArmatureFileInfo("Editor/Anim/timer/timer.ExportJson");
	animTimers = new Armature*[2];

	//Score
	text_main_score = static_cast<Text*>(Helper::seekWidgetByName(uiFromJson, "text_main_score"));
	text_main_score->setText("0");

	text_left_score = static_cast<Text*>(Helper::seekWidgetByName(uiFromJson, "text_left_score")); 
	text_left_score->setText("0");

	text_right_score = static_cast<Text*>(Helper::seekWidgetByName(uiFromJson, "text_right_score"));
	text_right_score->setText("0");

	//Life
	lifeArray = new ImageView*[MAX_LIFE];
	char *str = new char[1];
	for (int i = 0; i < MAX_LIFE; i++){
		sprintf(str, "life_%d", i);
		lifeArray[i] = static_cast<ImageView*>(Helper::seekWidgetByName(uiFromJson, str));
	}

	//Left --------------------------------------------
	text_left_puzzle = static_cast<Text*>(Helper::seekWidgetByName(uiFromJson, "text_left_puzzle"));

	image_left_puzzle = static_cast<ImageView*>(Helper::seekWidgetByName(uiFromJson, "image_left_puzzle"));

	button_left_answer_0 = static_cast<Button*>(Helper::seekWidgetByName(uiFromJson, "button_left_answer_0"));
	button_left_answer_0->addTouchEventListener(this, toucheventselector(GamePlayScene::answer));
	
	button_left_answer_1 = static_cast<Button*>(Helper::seekWidgetByName(uiFromJson, "button_left_answer_1"));
	button_left_answer_1->addTouchEventListener(this, toucheventselector(GamePlayScene::answer));

	button_left_answer_2 = static_cast<Button*>(Helper::seekWidgetByName(uiFromJson, "button_left_answer_2"));
	button_left_answer_2->addTouchEventListener(this, toucheventselector(GamePlayScene::answer));

	//Right --------------------------------------------
	text_right_puzzle = static_cast<Text*>(Helper::seekWidgetByName(uiFromJson, "text_right_puzzle"));

	image_right_puzzle = static_cast<ImageView*>(Helper::seekWidgetByName(uiFromJson, "image_right_puzzle"));

	button_right_answer_0 = static_cast<Button*>(Helper::seekWidgetByName(uiFromJson, "button_right_answer_0"));
	button_right_answer_0->addTouchEventListener(this, toucheventselector(GamePlayScene::answer));

	button_right_answer_1 = static_cast<Button*>(Helper::seekWidgetByName(uiFromJson, "button_right_answer_1"));
	button_right_answer_1->addTouchEventListener(this, toucheventselector(GamePlayScene::answer));

	button_right_answer_2 = static_cast<Button*>(Helper::seekWidgetByName(uiFromJson, "button_right_answer_2"));
	button_right_answer_2->addTouchEventListener(this, toucheventselector(GamePlayScene::answer));

	animTimers[0] = Armature::create("timer");
	
	animTimers[0]->setAnchorPoint(Vec2(0.5, 0.5));
	animTimers[0]->setPosition(50, 590);
	

	animTimers[1] = Armature::create("timer");
	
	animTimers[1]->setAnchorPoint(Vec2(0.5, 0.5));
	animTimers[1]->setPosition(905, 590);

	addChild(animTimers[0]);
	addChild(animTimers[1]);

	animTimers[0]->getAnimation()->playByIndex(0);
	animTimers[1]->getAnimation()->playByIndex(1);

	//Load question
	loadQuestionLeft(currentQuestionLeft, "JustMath");
	loadQuestionRight(currentQuestionRight, "Operations");


    return true;
}

void GamePlayScene::answer(Ref* pSender, TouchEventType type){
	if(type != TOUCH_EVENT_ENDED) return;
	char *str = new char[8];

	int tag = ((Button*)pSender)->getTag();
	if (tag < 10) { //Left
		if (tag == 1){ //right answer
			leftScore += 100;
			sprintf(str, "%d", leftScore);
			text_left_score->setText(str);
			totalScore = leftScore + rightScore;
			sprintf(str, "%d", totalScore);
			text_main_score->setText(str);
		} else {
			numLife--;
			lifeArray[numLife]->setVisible(false);
		}
		currentQuestionLeft++;
		loadQuestionLeft(currentQuestionLeft, "JustMath");
	}
	else{ //Right
		if (tag == 11){ //right answer
			rightScore += 100;
			sprintf(str, "%d", rightScore);
			text_right_score->setText(str);
			totalScore = leftScore + rightScore;
			sprintf(str, "%d", totalScore);
			text_main_score->setText(str);
		} else{
			numLife--;
			lifeArray[numLife]->setVisible(false);
		}
		currentQuestionRight++;
		loadQuestionRight(currentQuestionRight, "Operations");
	}

}

void GamePlayScene::loadQuestionLeft(int index, const char* type){
	//Left
	char* str = new char[50];

	sprintf(str, "%d", index);
	CCLOG("\n%s", str);
	text_left_puzzle->setText(str);

	sprintf(str, "Question/%s/q%d.png", type, index);
	CCLOG("\n%s", str);
	image_left_puzzle->loadTexture(str);

	std::vector<int> answerListIndex;
	answerListIndex.push_back(1);
	answerListIndex.push_back(2);
	answerListIndex.push_back(3);

	int answerIndex = Utils::getRandomIndex(3);
	sprintf(str, "Question/%s/q%d_%d.png", type, index, answerListIndex[answerIndex]);
	CCLOG("\n%d, %s", answerIndex, str);
	button_left_answer_0->setTag(answerListIndex[answerIndex]);
	answerListIndex.erase (answerListIndex.begin() + answerIndex);
	button_left_answer_0->loadTextureNormal(str);

	answerIndex = Utils::getRandomIndex(2);
	sprintf(str, "Question/%s/q%d_%d.png", type, index, answerListIndex[answerIndex]);
	CCLOG("\n%d, %s", answerIndex, str);
	button_left_answer_1->setTag(answerListIndex[answerIndex]);
	answerListIndex.erase (answerListIndex.begin() + answerIndex);
	button_left_answer_1->loadTextureNormal(str);

	answerIndex = 0;
	sprintf(str, "Question/%s/q%d_%d.png", type, index, answerListIndex[answerIndex]);
	CCLOG("\n%d, %s", answerIndex, str);
	button_left_answer_2->setTag(answerListIndex[answerIndex]);
	button_left_answer_2->loadTextureNormal(str);
}

void GamePlayScene::loadQuestionRight(int index, const char* type){
	//Right
	char* str = new char[50];

	sprintf(str, "%d", index);
	text_right_puzzle->setText(str);

	sprintf(str, "Question/%s/q%d.png", type, index);
	image_right_puzzle->loadTexture(str);

	std::vector<int> answerListIndex;
	answerListIndex.push_back(1);
	answerListIndex.push_back(2);
	answerListIndex.push_back(3);

	int answerIndex = Utils::getRandomIndex(3);
	sprintf(str, "Question/%s/q%d_%d.png", type, index, answerListIndex[answerIndex]);
	button_right_answer_0->setTag(10 + answerListIndex[answerIndex]);
	answerListIndex.erase(answerListIndex.begin() + answerIndex);
	button_right_answer_0->loadTextureNormal(str);

	answerIndex = Utils::getRandomIndex(2);
	sprintf(str, "Question/%s/q%d_%d.png", type, index, answerListIndex[answerIndex]);
	CCLOG("\n%d, %s", answerIndex, str);
	button_right_answer_1->setTag(10 + answerListIndex[answerIndex]);
	answerListIndex.erase(answerListIndex.begin() + answerIndex);
	button_right_answer_1->loadTextureNormal(str);

	answerIndex = 0;
	sprintf(str, "Question/%s/q%d_%d.png", type, index, answerListIndex[answerIndex]);
	button_right_answer_2->setTag(10 + answerListIndex[answerIndex]);
	button_right_answer_2->loadTextureNormal(str);
}
